# -*- coding: utf-8 -*-
import scrapy

from douban.items import BoosxiangqinItem


class BoostestSpider(scrapy.Spider):
    name = 'boostest'
    allowed_domains = ['https://www.zhipin.com']
    query = '软件测试工程师'
    city = '101280600'#深圳101280600  武汉101200100
    start_urls = ['https://www.zhipin.com/job_detail/?query='+query+'&scity='+city]

    def parse(self, response):
        job_list = response.xpath("//div[@class='job-list']/ul/li")
        for i in job_list:
            a = i.xpath(".//h3/a/@href").extract()[0]
            b = 'https://www.zhipin.com'+a
            yield scrapy.Request(b,callback=self.get_detail,dont_filter=True)
        next_link = response.xpath("//div[@class='page']/a[@class='next']/@href").extract()
        if next_link:
            next_link = next_link[0]
            yield scrapy.Request("https://www.zhipin.com" + next_link, callback=self.parse, dont_filter=True)


    def get_detail(self, response):
        item = BoosxiangqinItem()
        a = response.xpath("//div[@class='job-banner']//span[@class='badge']/text()").extract_first()
        p1 = ''.join(a.split())
        item['薪资'] = p1
        #item['release_time'] = response.xpath("//span[@class='time']/text()").extract_first()
        item['发布时间'] = response.xpath("//span[@class='time']/text()").extract_first()
        b = response.xpath("//div[@class='job-primary detail-box']/div[@class='info-primary']/p").extract_first()
        bt = b.split('<em class="vline"></em>')
        item['工作年限'] = bt[1]
        item['学历要求'] = bt[2]
        item['公司'] = response.xpath("//div[@class='info-company']/h3/a/text()").extract_first()
        item['工作地点'] = response.xpath("//div[@class='location-address']/text()").extract_first()
        c = response.xpath("//div[@class='job-sec']/div[@class='text']").extract_first()
        p1 = ''.join(c.split('<div class="text">'))
        p2 = ''.join(p1.split('</div>')).replace('<br>','')
        p3 = ''.join(p2.split())
        item['职位描述'] = p3
        #item['Established']
        Established = response.xpath("//div[@class='job-sec']/div[@class='level-list']/li[@class='res-time']/text()").extract_first()
        if Established:
            item['公司成立时间'] = Established
        item['详情页的url'] = response.url
        yield item
